/** 主程序 */
(function(window , document) {

    function initPage(respText) {
        var app = KwDom().item('#app');
        app.innerHTML = Reader().toHtml(respText);
        var links = app.getElementsByTagName('a');
        KwDom().attr(links , 'target' , '_blank');
        var scripts = app.getElementsByTagName('script');
        var executeScript = [];
        for(var i=0; i<scripts.length; i++) {
            if(scripts[i].type.indexOf('babel') !== -1) {
                continue;
            }
            executeScript.push(scripts[i].innerHTML);
            scripts[i].parentNode.removeChild(scripts[i]);
        }
        KwDom().executeScript(executeScript);
        _loadVue();
        _complipeBabel();
    }

    function _loadVue() {
        var vueData = window.kwdoc.vue || {};
        var vueLoader = [];
        for(var keyEl in  vueData) {
            var el = {
                el: keyEl,
            }
            for(var vd in vueData[keyEl]) {
                el[vd] = vueData[keyEl][vd];
            }
            vueLoader.push(el);
        }
        if(vueLoader.length === 0) {
            return;
        }
        KwDom().addScript('//cdn.jsdelivr.net/npm/vue@2/dist/vue.min.js' , function() {   
            if(typeof Vue !== 'undefined') {
                // new Vue();
                vueLoader.forEach(function(option) {
                    new Vue(option);
                });
            } 
        });
    }
    function _complipeBabel() {
        var app = KwDom().item('#app');
        var scripts = app.getElementsByTagName('script');
        var reactCodes = [];
        for(var i=0; i<scripts.length; i++) {
            if(scripts[i].type.indexOf('babel') !== -1) {
                reactCodes.push(scripts[i].innerHTML);
                scripts[i].parentNode.removeChild(scripts[i]);
            }
        }
        if(reactCodes.length == 0) {
            return ;
        }
        KwDom().addScript('//unpkg.com/@babel/standalone@7.13.17/babel.min.js' , function() {   
            if(typeof Babel !== 'undefined') {
                // 判断Babel是否识别
                var _complipeCode = [];
                reactCodes.forEach(function(code) {
                    var output = Babel.transform(code, { presets: [ "env","react"] }).code;
                    _complipeCode.push(output);
                });
                _loadReact(_complipeCode);
            }
        });
    }
    function _loadReact(_complipeCode) {
        var loadCount = 2;
        KwDom().addScript('//unpkg.com/react@16/umd/react.production.min.js' , function() {   
            loadCount --;
            _runCode();
        });
        KwDom().addScript('//unpkg.com/react-dom@16/umd/react-dom.production.min.js' , function() {   
            loadCount --;
            _runCode();
        });

        function _runCode() {
            console.log(loadCount);
            if(loadCount == 0) {
                KwDom().executeScript(_complipeCode);
            }
        }
    }

    var checkCount = 0;
    function _handlePreLoads(callback) {
        if(window.kwdoc.preloads) {
            var preloads = window.kwdoc.preloads;
            for(var key in  preloads) {
                var loaderItems = preloads[key] || [];
                checkCount += loaderItems.length;
                console.log('checkCount = ' + checkCount);
                for(var i=0; i<loaderItems.length; i++) {
    
                    if(loaderItems[i].src) {
    
                        var loadUrl = loaderItems[i].src;
                        delete loaderItems[i].src;
                        KwDom().addScript(loadUrl , function() {
                            checkCount --;
                            _checkFinish(callback);
                        }, loaderItems[i]);
                        
                    } else if(loaderItems[i].href) {
    
                        var loadUrl = loaderItems[i].href;
                        delete loaderItems[i].href;
                        KwDom().addLink(loadUrl , function() {
                            checkCount --;
                            _checkFinish(callback);
                        }, loaderItems[i]);
    
                    }
                }
            }
            return ;
        }
        callback && callback();
    }

    function _checkFinish(callback) {
        if(checkCount == 0) {
            callback && callback();
        }
    }

    function _loadContent() {
        ajaxReq({
            url: kwdoc.home || '',
            onPathErr: function() {
                // 未找到
                console.log(1)
            },
            onSucc: function(respText) {
                // 请求成功
                initPage(respText);
            },
            onErr: function() {
                // 请求错误
            }
        });
    }
    
    window.onload = function() {
        _handlePreLoads(function() {
            _loadContent();
        });
        
    }
})(window , document);